{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "提供了6万张28*28像素点的衣裤图片和标签，用于训练。\n",
    "\n",
    "提供1万张28*28像素点的衣裤图片和标签，用于测试。\n",
    "\n",
    "label:0-1\n",
    "\n",
    "Description:T恤、裤子、套头衫、连衣裙、外套、凉鞋、衬衫、运动鞋、包、靴子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 使用Sequent网络结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/5\n",
      "60000/60000 [==============================] - 6s 96us/sample - loss: 0.5063 - sparse_categorical_accuracy: 0.8213 - val_loss: 0.4179 - val_sparse_categorical_accuracy: 0.8502\n",
      "Epoch 2/5\n",
      "60000/60000 [==============================] - 5s 85us/sample - loss: 0.3795 - sparse_categorical_accuracy: 0.8628 - val_loss: 0.3956 - val_sparse_categorical_accuracy: 0.8568\n",
      "Epoch 3/5\n",
      "60000/60000 [==============================] - 5s 86us/sample - loss: 0.3372 - sparse_categorical_accuracy: 0.8770 - val_loss: 0.3666 - val_sparse_categorical_accuracy: 0.8683\n",
      "Epoch 4/5\n",
      "60000/60000 [==============================] - 5s 84us/sample - loss: 0.3138 - sparse_categorical_accuracy: 0.8838 - val_loss: 0.3550 - val_sparse_categorical_accuracy: 0.8726\n",
      "Epoch 5/5\n",
      "60000/60000 [==============================] - 5s 85us/sample - loss: 0.2958 - sparse_categorical_accuracy: 0.8921 - val_loss: 0.3529 - val_sparse_categorical_accuracy: 0.8728\n",
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten_1 (Flatten)          multiple                  0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              multiple                  100480    \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              multiple                  1290      \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "fashion = tf.keras.datasets.fashion_mnist\n",
    "(x_train,y_train),(x_test,y_test) = fashion.load_data()\n",
    "\n",
    "x_train,x_test = x_train / 255.0 ,x_test / 255.0\n",
    "\n",
    "model = tf.keras.models.Sequential([\n",
    "    tf.keras.layers.Flatten(),\n",
    "    tf.keras.layers.Dense(128,activation='relu'),\n",
    "    tf.keras.layers.Dense(10,activation='softmax')\n",
    "])\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy']\n",
    ")\n",
    "\n",
    "model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=1)\n",
    "\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 使用类class网络结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/5\n",
      "60000/60000 [==============================] - 6s 101us/sample - loss: 0.4940 - sparse_categorical_accuracy: 0.8253 - val_loss: 0.4285 - val_sparse_categorical_accuracy: 0.8465\n",
      "Epoch 2/5\n",
      "60000/60000 [==============================] - 6s 102us/sample - loss: 0.3709 - sparse_categorical_accuracy: 0.8676 - val_loss: 0.4105 - val_sparse_categorical_accuracy: 0.8526\n",
      "Epoch 3/5\n",
      "60000/60000 [==============================] - 6s 96us/sample - loss: 0.3337 - sparse_categorical_accuracy: 0.8781 - val_loss: 0.3698 - val_sparse_categorical_accuracy: 0.8655\n",
      "Epoch 4/5\n",
      "60000/60000 [==============================] - 5s 89us/sample - loss: 0.3103 - sparse_categorical_accuracy: 0.8868 - val_loss: 0.3968 - val_sparse_categorical_accuracy: 0.8562\n",
      "Epoch 5/5\n",
      "60000/60000 [==============================] - 5s 87us/sample - loss: 0.2941 - sparse_categorical_accuracy: 0.8909 - val_loss: 0.3438 - val_sparse_categorical_accuracy: 0.8745\n",
      "Model: \"fashion_model_14\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten_16 (Flatten)         multiple                  0         \n",
      "_________________________________________________________________\n",
      "dense_32 (Dense)             multiple                  100480    \n",
      "_________________________________________________________________\n",
      "dense_33 (Dense)             multiple                  1290      \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.keras.layers import Dense, Flatten\n",
    "from tensorflow.keras import Model\n",
    "\n",
    "\n",
    "fashion = tf.keras.datasets.fashion_mnist\n",
    "(x_train,y_train),(x_test,y_test) = fashion.load_data()\n",
    "\n",
    "x_train,x_test = x_train / 255.0 ,x_test / 255.0\n",
    "\n",
    "class FashionModel(Model):\n",
    "    def __init__(self):\n",
    "        super(FashionModel,self).__init__()\n",
    "        self.flatten=Flatten()\n",
    "        self.d1=Dense(128,activation='relu')\n",
    "        self.d2=Dense(10,activation='softmax')\n",
    "    def call(self,x):\n",
    "        x=self.flatten(x)\n",
    "        x=self.d1(x)\n",
    "        y=self.d2(x)\n",
    "        return y\n",
    "    \n",
    "model = FashionModel()\n",
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy'])\n",
    "\n",
    "model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)\n",
    "model.summary()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
